package fule.com.appbarlayoutdemo.base;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者： njb
 * 时间： 2018/12/26 21:03
 * 描述：
 * 来源：
 */
public  abstract class BaseFragment extends Fragment {
    public Context context;
    private ProgressDialog dialog;

    // 控件是否初始化完成
    protected boolean isViewCreated;

    // 当前fragment是否加载过数据,如加载过数据，则不再加载
    protected boolean isLoadCompleted;
    //是不是可见
    protected boolean isUIVisible;

    protected Unbinder unbinder;



    // 懒加载,强制子类重写
    protected abstract void loadData();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void addListener();


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isUIVisible = isVisibleToUser;
        if (isVisibleToUser && isViewCreated && isUIVisible && !isLoadCompleted) {
            isLoadCompleted = true;
            loadData();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isViewCreated && isUIVisible) {
            loadData();
            isLoadCompleted = true;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);

        initView();
        addListener();
        isViewCreated = true;
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isUIVisible = !hidden;
        isLoadCompleted = !hidden;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    public void onResume() {
        super.onResume();

    }

    public void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();


        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 打开指定的activity
     *
     * @param cls
     */
    public void startA(@NonNull Class<?> cls) {
        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }







    private void showFileDialog() {
    }

    private void hideFileDialog() {
    }





}
